{% extends 'dns_manager/base.html' %}

{% block title %}DNS记录管理 - {{ domain.name }} - DNS管理系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item">
                    <a href="{% url 'domains' %}" class="text-decoration-none">
                        <i class="bi bi-globe me-1"></i>域名管理
                    </a>
                </li>
                <li class="breadcrumb-item active">{{ domain.name }}</li>
            </ol>
        </nav>
        <h2 class="mb-1 mt-2">
            <i class="bi bi-dns text-info me-2"></i>DNS解析记录
        </h2>
        <p class="text-muted mb-0">管理 {{ domain.name }} 的DNS解析记录</p>
    </div>
    <div class="d-flex gap-2">
        <a href="{% url 'sync_domain_records' domain.id %}" 
           class="btn btn-outline-info"
           onclick="return confirm('确定要从云端同步DNS记录吗？')">
            <i class="bi bi-arrow-repeat me-1"></i>同步记录
        </a>
        <a href="{% url 'create_dns_record' domain.id %}" class="btn btn-primary">
            <i class="bi bi-plus me-1"></i>添加记录
        </a>
    </div>
</div>

<!-- 域名信息卡片 -->
<div class="card border-0 shadow-sm mb-4">
    <div class="card-body">
        <div class="row align-items-center">
            <div class="col-md-8">
                <div class="d-flex align-items-center">
                    <div class="domain-icon bg-primary bg-gradient text-white rounded-circle me-3">
                        <i class="bi bi-globe"></i>
                    </div>
                    <div>
                        <h5 class="mb-1">{{ domain.name }}</h5>
                        <div class="d-flex align-items-center">
                            {% if domain.cloud_account.provider == 'aliyun' %}
                                <span class="badge bg-primary me-2">阿里云</span>
                            {% elif domain.cloud_account.provider == 'tencent' %}
                                <span class="badge bg-success me-2">腾讯云</span>
                            {% elif domain.cloud_account.provider == 'huawei' %}
                                <span class="badge bg-warning me-2">华为云</span>
                            {% elif domain.cloud_account.provider == 'volcengine' %}
                                <span class="badge bg-danger me-2">火山引擎</span>
                            {% endif %}
                            <span class="text-muted">{{ domain.cloud_account.name }}</span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4 text-end">
                <div class="row text-center">
                    <div class="col-6">
                        <div class="text-primary mb-1">
                            <strong>{{ records.paginator.count }}</strong>
                        </div>
                        <small class="text-muted">DNS记录</small>
                    </div>
                    <div class="col-6">
                        <div class="text-success mb-1">
                            <strong>{{ domain.last_sync|date:"m-d H:i"|default:"-" }}</strong>
                        </div>
                        <small class="text-muted">最后同步</small>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

{% if records.object_list %}
<div class="card border-0 shadow-sm">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="bg-light">
                    <tr>
                        <th class="border-0 ps-4">主机记录</th>
                        <th class="border-0">记录类型</th>
                        <th class="border-0">记录值</th>
                        <th class="border-0">TTL</th>
                        <th class="border-0">优先级</th>
                        <th class="border-0">状态</th>
                        <th class="border-0 text-center">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for record in records %}
                    <tr>
                        <td class="ps-4">
                            <div class="fw-semibold">{{ record.name }}</div>
                            {% if record.record_id %}
                                <small class="text-muted">ID: {{ record.record_id }}</small>
                            {% endif %}
                        </td>
                        <td>
                            {% if record.record_type == 'A' %}
                                <span class="badge bg-primary">{{ record.record_type }}</span>
                            {% elif record.record_type == 'AAAA' %}
                                <span class="badge bg-info">{{ record.record_type }}</span>
                            {% elif record.record_type == 'CNAME' %}
                                <span class="badge bg-success">{{ record.record_type }}</span>
                            {% elif record.record_type == 'MX' %}
                                <span class="badge bg-warning">{{ record.record_type }}</span>
                            {% elif record.record_type == 'TXT' %}
                                <span class="badge bg-secondary">{{ record.record_type }}</span>
                            {% else %}
                                <span class="badge bg-light text-dark">{{ record.record_type }}</span>
                            {% endif %}
                        </td>
                        <td>
                            <span class="text-muted text-truncate d-inline-block" style="max-width: 300px;">
                                {{ record.value }}
                            </span>
                        </td>
                        <td>
                            <span class="text-muted">{{ record.ttl }}s</span>
                        </td>
                        <td>
                            {% if record.record_type == 'MX' %}
                                <span class="text-primary">{{ record.priority }}</span>
                            {% else %}
                                <span class="text-muted">-</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if record.is_active %}
                                <span class="badge bg-success">
                                    <i class="bi bi-check-circle me-1"></i>启用
                                </span>
                            {% else %}
                                <span class="badge bg-secondary">
                                    <i class="bi bi-pause-circle me-1"></i>禁用
                                </span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="text-center">
                                <div class="btn-group btn-group-sm" role="group">
                                    <button class="btn btn-outline-primary" 
                                            onclick="copyToClipboard('{{ record.value }}')"
                                            title="复制记录值">
                                        <i class="bi bi-copy"></i>
                                    </button>
                                    <div class="btn-group btn-group-sm" role="group">
                                        <button class="btn btn-outline-secondary dropdown-toggle" 
                                                data-bs-toggle="dropdown">
                                            <i class="bi bi-three-dots"></i>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <li>
                                                <a class="dropdown-item" href="#">
                                                    <i class="bi bi-pencil me-2"></i>编辑记录
                                                </a>
                                            </li>
                                            <li><hr class="dropdown-divider"></li>
                                            <li>
                                                <a class="dropdown-item text-danger" href="#"
                                                   onclick="return confirm('确定要删除此DNS记录吗？')">
                                                    <i class="bi bi-trash me-2"></i>删除记录
                                                </a>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- 分页 -->
    {% if records.has_other_pages %}
    <div class="card-footer bg-white border-0">
        <nav aria-label="DNS记录分页">
            <ul class="pagination pagination-sm justify-content-center mb-0">
                {% if records.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ records.previous_page_number }}">
                            <i class="bi bi-chevron-left"></i>
                        </a>
                    </li>
                {% endif %}
                
                {% for num in records.paginator.page_range %}
                    {% if records.number == num %}
                        <li class="page-item active">
                            <span class="page-link">{{ num }}</span>
                        </li>
                    {% elif num > records.number|add:'-3' and num < records.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                        </li>
                    {% endif %}
                {% endfor %}
                
                {% if records.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ records.next_page_number }}">
                            <i class="bi bi-chevron-right"></i>
                        </a>
                    </li>
                {% endif %}
            </ul>
        </nav>
    </div>
    {% endif %}
</div>
{% else %}
<div class="text-center py-5">
    <div class="mb-4">
        <i class="bi bi-dns text-muted" style="font-size: 4rem;"></i>
    </div>
    <h4 class="text-muted mb-3">暂无DNS记录</h4>
    <p class="text-muted mb-4">
        为 {{ domain.name }} 添加第一条DNS解析记录
    </p>
    <div class="d-flex gap-2 justify-content-center">
        <a href="{% url 'create_dns_record' domain.id %}" class="btn btn-primary btn-lg">
            <i class="bi bi-plus me-2"></i>添加记录
        </a>
        <a href="{% url 'sync_domain_records' domain.id %}" 
           class="btn btn-outline-info btn-lg"
           onclick="return confirm('确定要从云端同步DNS记录吗？')">
            <i class="bi bi-arrow-repeat me-2"></i>同步记录
        </a>
    </div>
</div>
{% endif %}

<style>
.domain-icon {
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
}

.table tbody tr:hover {
    background-color: rgba(0, 123, 255, 0.05);
}

.text-truncate {
    display: inline-block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
</style>

<script>
function copyToClipboard(text) {
    if (navigator.clipboard) {
        navigator.clipboard.writeText(text).then(function() {
            // 显示成功消息
            const toast = document.createElement('div');
            toast.className = 'toast-message bg-success text-white p-2 rounded position-fixed';
            toast.style.top = '20px';
            toast.style.right = '20px';
            toast.style.zIndex = '9999';
            toast.textContent = '已复制到剪贴板';
            document.body.appendChild(toast);
            
            setTimeout(() => {
                toast.remove();
            }, 2000);
        });
    }
}
</script>
{% endblock %}